package com.cnt.service.impl;

import com.cnt.dao.DonationsInfoDao;
import com.cnt.entity.DonationsInfo;
import com.cnt.service.DonationsInfoService;
import com.cnt.util.FileUtil;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
public class DonationsInfoServiceImpl implements DonationsInfoService {

    @Autowired
    private DonationsInfoDao donationsInfoDao;
    @Autowired
    private FileUtil fileUtil;

    @Override
    public ResultVo<DonationsInfo> getDonationsInfo(Integer donationId) {
        DonationsInfo donationsInfo = donationsInfoDao.selectByDonationId(donationId);
        if(donationsInfo==null)  return new ResultVo<>(1,"没有查询到结果",null);
        else return new ResultVo<>(0,"查询成功",donationsInfo);
    }

    @Override
    public ResultVo<DonationsInfo> updateDetails(DonationsInfo donationsInfo) {
        Integer donationId = donationsInfo.getDonationId();
        DonationsInfo info = donationsInfoDao.selectByDonationId(donationId);
        info.setIncident(donationsInfo.getIncident());
        info.setFamily(donationsInfo.getFamily());
        info.setFinance(donationsInfo.getFinance());
        info.setStatus(donationsInfo.getStatus());
        int i = donationsInfoDao.updateById(info);
        if (i != 1 ) return new ResultVo<>(1, "系统繁忙，请稍后再试", null);
        return new ResultVo<>(0, "修改成功", info);
    }

    @Override
    public ResultVo<String> uploadData(Integer donationId,MultipartFile file){

        // 校验上传的文件大小
        long size = file.getSize();
        if (size > 16 * 1024 * 1024) { // 限制文件大小为16MB
            return new ResultVo<>(4, "文件大小不能超过16MB", null);
        }
        String contentType = file.getContentType();
        DonationsInfo donationsInfo = donationsInfoDao.selectByDonationId(donationId);
        try {
            File toFile = fileUtil.convertMultipartFileToFile(file);
            if (contentType != null) {
                if (contentType.equals("application/msword") || contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                    File toPdf = fileUtil.convertToPdf(toFile);
                    byte[] bytes = fileUtil.addWatermarkToPDF(toPdf, "捐赠平台专用");
                    donationsInfo.setFile(bytes);
                } else if (contentType.equals("application/pdf")) {
                    byte[] bytes = fileUtil.addWatermarkToPDF(toFile, "捐赠平台专用");
                    donationsInfo.setFile(bytes);
                } else {
                    return new ResultVo<>(2, "文件不是word或者pdf类型，上传失败", null);
                }
            } else {
                return new ResultVo<>(3, "文件未知类型，上传失败", null); // 未知类型
            }
            toFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = donationsInfoDao.updateById(donationsInfo);
        if (i != 1) return new ResultVo<>(1, "系统繁忙，请稍后再试", null);
        return new ResultVo<>(0, "上传成功", null);
    }

    @Override
    public byte[] downloadDonationInfoFile(Integer donationId) {
        DonationsInfo donationsInfo = donationsInfoDao.getFileByDonationId(donationId);
        return donationsInfo.getFile();
    }


}
